package com.lagou.filter;

import com.lagou.data.DataListUtil;
import com.lagou.data.MethodBeanInfo;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

@Activate(group = {CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {
    private volatile int counts = 0;

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long startTime = System.currentTimeMillis();
        try {
            // 执行方法
            return invoker.invoke(invocation);
        } finally {
            String methodName = invocation.getMethodName();
            long longTime = System.currentTimeMillis() - startTime;
            MethodBeanInfo info = new MethodBeanInfo(methodName, longTime, System.currentTimeMillis());
            DataListUtil.put(methodName, info);
            System.out.println("方法" + methodName + "-----" + "耗时:" + longTime + "毫秒");
        }
    }
}
